package com.tcm.pay.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.base.core.utils.GeneratorUtils;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.tcm.pay.dao.mapper.PaymentMapper;
import com.tcm.pay.domain.dto.PaymentDTO;
import com.tcm.pay.entity.Payment;
import com.tcm.pay.enums.BusinessCodeEnum;
import com.tcm.pay.service.PaymentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @Description:支付类型服务类
 * 2015年4月4日下午9:29:26
 *
 */
@Service
public class PaymentServiceImpl implements PaymentService {


    private final PaymentMapper paymentMapper;

	@DubboReference(version = "0.0.1-beta",group = "cached-uid",timeout = 6000, check = false,retries = 3)
	private UidGenerator uidGenerator;

	@Autowired
	public PaymentServiceImpl(PaymentMapper paymentMapper) {
		this.paymentMapper = paymentMapper;
	}


	@Override
	public PaymentDTO findByOrderId(Long uid, Long orderId, String tradeType) throws Exception {
		Payment payment = paymentMapper.findByOrderId(uid, orderId, tradeType);
		if(ObjectUtil.isEmpty(payment)){
			return null;
		}
		PaymentDTO paymentDTO = new PaymentDTO();
		BeanUtils.copyProperties(payment, paymentDTO);
		return paymentDTO;
	}


	@Override
	public PageBean<PaymentDTO> listPagePayment(Long uid, String tradeType, Long page, Long pagesize) throws Exception {
		Long count = paymentMapper.countByUserId(uid, tradeType);
		PageContext.setDataCount(count);
		PageContext.setCurrentPage(page);
		PageContext.setPageSize(pagesize);
		if(count.intValue() == 0){
			return new PageBean<>(null);
		}
		List<Payment> payments = paymentMapper.listPageByUserId(uid, tradeType, page, pagesize);
		List<PaymentDTO> paymentDTOS = DozerConvert.copyList(payments, PaymentDTO.class);
		PageContext.clear();
		return new PageBean<>(paymentDTOS);
	}

	@Override
	public int updateStatusToPaid(Long userId, Long orderId, String tradeType) throws Exception {
		try{
			Payment payment = new Payment();
			payment.setTargetId(userId);
			payment.setDataId(orderId);
			payment.setDataSrc(tradeType);
			payment.setStatus(new Byte("1"));
			payment.setUpdateTime(DateUtil.date());
			paymentMapper.updateStatus(payment);
		}catch (Exception e){
			R.throwFail(e.getMessage());
		}
		return 1;
	}

	//创建支付单
	@Override
	public void create(PaymentDTO paymentDto) throws Exception {
		if(ObjectUtil.isEmpty(paymentDto.getTargetId())){
			R.throwFail(BusinessCodeEnum.BUYER_ID_NOT_EXISTS);
		}
		if(ObjectUtil.isEmpty(paymentDto.getDataId())){
			R.throwFail(BusinessCodeEnum.ID_NOT_EXISTS);
		}
		Payment payment = paymentMapper.findByOrderId(paymentDto.getTargetId(),
				paymentDto.getDataId(), paymentDto.getDataSrc());
		if(ObjectUtil.isNull(payment)) {
			Payment paymentInsert = DozerConvert.copyProperties(paymentDto, Payment.class);
			paymentInsert.setId(uidGenerator.getUID());
			paymentInsert.setPaySn(GeneratorUtils.genOrderId("01", "2"));
			paymentInsert.setCreateTime(DateUtil.date());
			paymentInsert.setStatus(new Byte("0"));
			paymentInsert.setUpdateTime(DateUtil.date());
			paymentMapper.insert(paymentInsert);
		}
	}

	@Override
	public void delete(Long userId, Long orderId, String tradeType) throws Exception{
		paymentMapper.deleteByUserId(userId, orderId, tradeType);
	}

	@Override
	public PaymentDTO findByPaySn(String paySn, String tradeType) {
		Payment payment = paymentMapper.findByPaySn(paySn, tradeType);
		if(ObjectUtil.isEmpty(payment)){
			return null;
		}
		PaymentDTO paymentDTO = DozerConvert.copyProperties(payment, PaymentDTO.class);
		return paymentDTO;
	}

	/**
	 * 创建新的支付单
	 * @param orderId
	 * @param buyerId
	 * @return
	 * @throws Exception
	 */
	@Override
	public void createNewPaySn(Long orderId, Long buyerId, String tradeType) throws Exception{
		if(ObjectUtil.isEmpty(orderId)){
			R.throwFail(BusinessCodeEnum.ID_NOT_EXISTS);
		}
		if(ObjectUtil.isEmpty(buyerId)){
			R.throwFail(BusinessCodeEnum.BUYER_ID_NOT_EXISTS);
		}
		Payment payment = paymentMapper.findByOrderId(buyerId, orderId, tradeType);
		if(ObjectUtil.isEmpty(payment)){
			R.throwFail(BusinessCodeEnum.ORDER_PARAM_DATA_NOT_EXISTS);
		}

		//创建新的订单
		payment.setPaySn(GeneratorUtils.genOrderId("01", "2"));
		payment.setCreateTime(DateUtil.date());
		payment.setStatus(new Byte("0"));
		payment.setUpdateTime(DateUtil.date());
		paymentMapper.update(payment);
	}

}
